package com.gx303.wximageselect;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gx303.wximageselect.Inter.ImageGridSelectInterface;
import com.gx303.wximageselect.Inter.ImageSelectInterface;
import com.gx303.wximageselect.adapter.ImageGridAdapter;
import com.gx303.wximageselect.adapter.ImageRecycleAdapter;
import com.gx303.wximageselect.bean.Folder;
import com.gx303.wximageselect.bean.Image;
import com.gx303.wxselectlibrary.R;
import java.util.ArrayList;


public class PopWxImageSelectFromXc extends Activity {
    /**
     * 最大图片选择次数，int类型
     */
    public static final String EXTRA_SELECT_COUNT = "max_select_count";
    /**
     * 选择的文件夹
     */
    public static final String EXTRA_SELECT_FOLDER="select_folder";
    public static final String EXTRA_ALL_OR_FOLDER="all_or_folder";
    
    boolean isAll=false;
    LinearLayout lay_back;//返回
    LinearLayout lay_cancel;//取消
    TextView tv_title;//标题
    private android.support.v7.widget.RecyclerView recyclerView;
    private TextView tv_selectImageNum;//选择个数的按钮
    private Button btn_preview;//预览按钮
    private Button btn_send;//发送
    private int mDesireImageCount;
    final int REQUSET_PREVIEW=9809001;

    public   ArrayList<Image> images ;
    ImageGridAdapter adapter;
    @SuppressLint("NewApi")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_wx_image_select_from_xc);
        init();
//        datas.setSelectImageNull();
        
        isAll=getIntent().getBooleanExtra(EXTRA_ALL_OR_FOLDER, true);
        if(isAll)
        {
        	images=datas.images;
        	tv_title.setText(getResources().getString(R.string.wx_image_select_allimage));
        }
        else
        {
        	Folder selectFolder=(Folder)getIntent().getSerializableExtra(EXTRA_SELECT_FOLDER);
        	images=selectFolder.images;
        	tv_title.setText(selectFolder.name);
        }
        Log.e("Unity", "数据长度"+images.size());


        lay_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//点击返回 跳到文件夹选择
				Intent it_choice_folder=new Intent();
				it_choice_folder.setClass(getApplicationContext(), PopWxImageSelectFolder.class);
				startActivity(it_choice_folder);
				finish();
			}
		});
        
        GridLayoutManager mgr=new GridLayoutManager(this,4);  
        recyclerView.setLayoutManager(mgr);  
        adapter=new ImageGridAdapter(getApplicationContext(),images,new ImageGridSelectInterface() {
			
			@Override
			public void preview(int index) {
				// TODO Auto-generated method stub
//				Log.e("Unity", "预览"+im1.path);
				Intent it_preview=new Intent();
				it_preview.setClass(getApplicationContext(), PopWxImageSelectPreview.class);
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_DATA, images);
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_SELECT_IMAGE, adapter.getSelectImage());
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_NOW_SELECT_INDEX, index);
				startActivityForResult(it_preview, REQUSET_PREVIEW);
			}
			
			@Override
			public void clickTopRIght(Image im1) {
				// TODO Auto-generated method stub
				afterSelect();
			}
		});
        
        WindowManager wm = (WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        adapter.setHeight(width/4);
//        adapter.setSelectImage(datas.getSelectImage());
        recyclerView.setAdapter(adapter);
        
        //预览
        btn_preview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it_preview=new Intent();
				it_preview.setClass(getApplicationContext(), PopWxImageSelectPreview.class);
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_DATA, images);
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_SELECT_IMAGE, adapter.getSelectImage());
				it_preview.putExtra(PopWxImageSelectPreview.EXTRA_NOW_SELECT_INDEX, 0);
				startActivityForResult(it_preview, REQUSET_PREVIEW);
			}
		});
        //发送
        btn_send.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendImage(adapter.getSelectImage());
			}
		});
    }
    public void sendImage(ArrayList<Image> selectImages)
    {
		if(selectImages.size()==0)
		{
			Toast.makeText(getApplicationContext(), getString(R.string.wx_image_select_no_select_image), Toast.LENGTH_SHORT).show();
		}
		else
		{
			datas.imageSendInterface1.send(selectImages);
			finish();
		}
    }
    public void afterSelect()
    {
    	int num=adapter.SelectImageNum();
    	if(num==0)
    	{
    		tv_selectImageNum.setText("");
    	}
    	else
    	{
    		tv_selectImageNum.setText(String.valueOf(num));
    	}
    }
    public void init()
    {
        lay_back=(LinearLayout)findViewById(R.id.lay_back);
        lay_cancel=(LinearLayout)findViewById(R.id.lay_cancel);
        tv_title=(TextView)findViewById(R.id.tv_title);
        recyclerView=(android.support.v7.widget.RecyclerView)findViewById(R.id.recyclerView);
        tv_selectImageNum=(TextView)findViewById(R.id.tv_selectImageNum);
        btn_preview=(Button)findViewById(R.id.btn_preview);
        btn_send=(Button)findViewById(R.id.btn_send);
    }

 
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
//		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode)
		{
			case REQUSET_PREVIEW://预览
				if(resultCode==RESULT_CANCELED)
				{
					//点击了返回，或者点击了back
					ArrayList<Image> selectImages=(ArrayList<Image>)data.getSerializableExtra(PopWxImageSelectPreview.EXTRA_SELECT_IMAGE);
					adapter.setSelectImage(selectImages);
					adapter.notifyDataSetChanged();
					afterSelect();
				}
				else if(resultCode==RESULT_OK)
				{
					ArrayList<Image> selectImages=(ArrayList<Image>)data.getSerializableExtra(PopWxImageSelectPreview.EXTRA_SELECT_IMAGE);
					sendImage(selectImages);
				}
				break;
		}
	}


}
